package hhh.com.dianming;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Into_alt extends AppCompatActivity implements View.OnClickListener {
    EditText et_name;
    TextView tv_user;
    Button bt_alt;
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    String name,user,id;
    private MySqlite mySqlite;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_into_alt);

        sp = getSharedPreferences("user",MODE_PRIVATE);
        editor = sp.edit();
        et_name = (EditText) findViewById(R.id.et_name);
        tv_user = (TextView) findViewById(R.id.tv_user);
        id = sp.getString("id","");
        name = sp.getString("name","");
        user = sp.getString("user","");

        et_name.setText(name);
        tv_user.setText(user);
        bt_alt = (Button) findViewById(R.id.bt_alt);
        bt_alt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_alt:
                id = sp.getString("id","");
                name = et_name.getText().toString().trim();
                if(name.equals("")){
                    Toast.makeText(this,"姓名不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                mySqlite=new MySqlite(this);
                db=mySqlite.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("name",name);
                db.update("mings",values,"id=?",new String[]{id});
                Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
    }
}
